package Dao.Imp;

import Dao.UserVideoDao;
import Request.VideoRequest;
import modal.User;
import modal.UserVideo;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by tan on 2017/8/25.
 * 15:08
 */
@Repository
@Transactional
public class UserVideoDaoImp  implements UserVideoDao{

    @Autowired
    SessionFactory sessionFactory;

    public String addNewVideo(VideoRequest item) {
        Session session = sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql  = new StringBuilder();
            String id  = UUID.randomUUID().toString().replace("-","");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date =df.format(new Date());
            sql.append("insert into user_video (username,videosrc,date,videoname,videoid) values (?,?,?,?,?)");
            Query query = session.createSQLQuery(sql.toString());
            query.setString(0,item.getUsername());
            query.setString(1,item.getVideosrc());
            query.setString(2,date);
            query.setString(3,item.getVideoname());
            query.setString(4,id);
            if(query.executeUpdate() ==1) return "sucess";
        }catch (Exception e)
        {
            System.out.println("插入视频记录失败");
            e.printStackTrace();
        }
        return "error";
    }

    public String deleteVideo(String id) {
        Session session = sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("delete from user_video where videoid =?");
            Query query = session.createSQLQuery(sql.toString());
            query.setString(0,id);
            if(query.executeUpdate()==1) return "sucess";
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("删除video记录失败");
        }
        return "error";
    }

    public List getVideoByName(String name) {
        Session session = sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("select * from user_video where username=?");
            Query query = session.createSQLQuery(sql.toString()).setString(0,name);
            query.setResultTransformer(Transformers.aliasToBean(UserVideo.class));
            return query.list();
        }catch (Exception e)
        {
            System.out.println("获取列表失败");
            e.printStackTrace();
        }
        return null;
    }
    public UserVideo getUserVideo(String videoid) {
        Session session = sessionFactory.getCurrentSession();
        try
        {
            StringBuilder sql = new StringBuilder();
            sql.append("select * from user_video where videoid=?");
            Query query = session.createSQLQuery(sql.toString()).setString(0,videoid);
            query.setResultTransformer(Transformers.aliasToBean(UserVideo.class));
            return (UserVideo)query.uniqueResult();
        }catch (Exception e)
        {
            System.out.println("获取Video失败");
            e.printStackTrace();
        }
        return null;
    }
}
